import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from '../entity/appState.model';
import * as contactsActions from '../actions/contacts.action';
// native
import { Contacts } from '@ionic-native/contacts';

// utils
import { pinyin } from '../../utils/pinyin';
import { compareProps } from '../../utils/basic';

/*** 获取通讯录
 * */
@Injectable()
export class ContactsService {

  private testList= [
    {
      "_objectInstance": {
        "id": "1",
        "rawId": null,
        "displayName": "张三",
        "name": {"familyName": "张", "givenName": "三", "formatted": "三 张"},
        "nickname": null,
        "phoneNumbers": [{"id": "1", "pref": false, "value": "215884887888788", "type": "mobile"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "1"
    }, {
      "_objectInstance": {
        "id": "4",
        "rawId": null,
        "displayName": "赵无极",
        "name": {"familyName": "赵", "givenName": "无极", "formatted": "无极 赵"},
        "nickname": null,
        "phoneNumbers": [{"id": "8", "pref": false, "value": "125455", "type": "mobile"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "4"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "李琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "穆琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "弹琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "一琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "亢琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "熊琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "今琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "为琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "拼琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "看琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "是琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "人琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "好琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "的琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "成琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "吧琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }, {
      "_objectInstance": {
        "id": "5",
        "rawId": null,
        "displayName": "啊琦",
        "name": {"familyName": "李", "givenName": "琦", "formatted": "琦 李"},
        "nickname": null,
        "phoneNumbers": [{"id": "9", "pref": false, "value": "186 9679 0771"}],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null
      }, "rawId": "5"
    }];
  constructor(
    private contacts: Contacts,
    private store$: Store<AppState>,
  ) { }

  /*** 本地测试
  * */
  getLocalTestList()
  {
    this.formatCallback(this.testList);
  }
  /*** 查询所有联系人
   * */
  getContcatsList()
  {
    this.contacts.find(["displayName", "phoneNumbers"],{
      filter: '',
      multiple : true,
      hasPhoneNumber : true,
    }).then(data=> {
      debugger;
      this.formatCallback(data);
    }).catch(err=> {
      alert(err);
    });
  }

  /*** 处理返回的联系人回调
   * @param contacts 联系人信息
   * */
  formatCallback(contacts= []){

    // 保存格式化后的数组
    /*** obj 数据格式
     * @key name        用户名称
     * @key phone       电话号码
     * @key nameChar    用户名称拼音
     * @key camelChar   用户名称拼音首字母
    * */
    let contactsList= [];
    try {
      const arr= contacts.filter(item=> !!item.rawId && !!item._objectInstance.displayName && !!item._objectInstance.phoneNumbers[0].value);
      arr.forEach(value=>{
        let obj= {};
        obj['rawId']= value.rawId|| '';
        obj['name']= value._objectInstance.displayName|| '';
        obj['phone']= value._objectInstance.phoneNumbers[0].value|| '';
        obj['nameChar']= pinyin.getFullChars(obj['name']);
        const camelChar= pinyin.getCamelChars(obj['nameChar']).substring(0, 1).toUpperCase();

        let j = 0;
        const len = contactsList.length;
        for(j; j < len; j++) {
          if(contactsList[j].key == camelChar) {
            contactsList[j].value.push(obj);
            break;
          }
        }
        if(j == len) {
          // console.log('新增key');
          let arr = [];
          arr.push(obj);
          contactsList[len] = {
            key: camelChar,
            value: arr
          };
        }

        obj= null;
      });
    }catch (err){
      console.log(err);
    }
    // 首字母排序
    contactsList.sort(compareProps('key'));
    // 缓存联系人信息
    this.store$.dispatch(new contactsActions.ContactsCacheAction(contactsList))
  }
}
